import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../views/Main.vue";
import Home from "../views/Home.vue";
import Warehouse from "../views/Warehouse.vue";
import Experience from "../views/Experience.vue";
import Knowledge from "../views/Knowledge.vue";
import Video from "../views/Video.vue";
import Myhome from "../views/MyHome.vue";
import DiseaseDetails from "../views/DiseaseDetails.vue";
Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/main/home" },
  {
    path: "/main",
    name: "Main",
    component: Main,
    children: [
      {
        path: "home",
        name: "Home",
        component: Home,
        meta: {
          requireAuth: false,//是否必须登录
          title: '首页',
          keepalive:false//是否缓存
        },
      },
      {
        path: "warehouse",
        name: "Warehouse",
        component: Warehouse,
        meta: {
          requireAuth: false,
          title: '疾病库',
          keepalive:false
        },
      },
      {
        path: "experience",
        name: "Experience",
        component: Experience,
        meta: {
          requireAuth: false,
          title: '疾病经验'
        },
      },
      {
        path: "knowledge",
        name: "Knowledge",
        component: Knowledge,
        meta: {
          requireAuth: false,
          title: '疾病知识',
          keepalive:true
        },
      },
      {
        path: "video",
        name: "Video",
        component: Video,
        meta: {
          requireAuth: false,
          title: '疾病视频'
        },
      },
      {
        path: "MyHome",
        name: "MyHome",
        component: Myhome,
        meta: {
          requireAuth: true,
          title: '我的主页',
          keepalive:false
        },
        children: [
          {
            path: "mydisease",
            name: "mydisease",
            component: resolve => require(['../components/myHome/MyDisease.vue'], resolve),
            meta: {
              title: '收藏疾病',
            },
          },
          {
            path: "myhistory",
            name: "myhistory",
            component: resolve => require(['../components/myHome/MyHistory.vue'], resolve),
            meta: {
              title: '收藏历史',
            },
          },
          {
            path: "myknowledeg",
            name: "myknowledeg",
            component: resolve => require(['../components/myHome/MyKnowledeg.vue'], resolve),
            meta: {
              title: '收藏知识',
            },
          },
          {
            path: "myvideo",
            name: "myvideo",
            component: resolve => require(['../components/myHome/MyVideo.vue'], resolve),
            meta: {
              title: '收藏视频',
            },
          },
          {
            path: "myall",
            name: "myall",
            component: resolve => require(['../components/myHome/MyAll.vue'], resolve),
            meta: {
              title: '主页',
            },
          },
          {
            path: "myconcern",
            name: "myconcern",
            component: resolve => require(['../components/myHome/MyConcern.vue'], resolve),
            meta: {
              title: '关注列表',
            },
          },
          {
            path: "myfans",
            name: "myfans",
            component: resolve => require(['../components/myHome/MyFans.vue'], resolve),
            meta: {
              title: '粉丝列表',
            },
          },
        ]
      },
      {
        path: "herhome",
        name: "herhome",
        component:  resolve => require(['../views/HerHome.vue'], resolve),
        meta: {
          requireAuth: false,
          title: '他人主页'
        },
        children: [
          {
            path: "mydisease",
            name: "mydiseaseher",
            component: resolve => require(['../components/myHome/MyDisease.vue'], resolve),
            meta: {
              title: '收藏疾病',
            },
          },
          {
            path: "myhistory",
            name: "myhistoryher",
            component: resolve => require(['../components/myHome/MyHistory.vue'], resolve),
            meta: {
              title: '收藏历史',
            },
          },
          {
            path: "myknowledeg",
            name: "myknowledegher",
            component: resolve => require(['../components/myHome/MyKnowledeg.vue'], resolve),
            meta: {
              title: '收藏知识',
            },
          },
          {
            path: "myvideo",
            name: "myvideoher",
            component: resolve => require(['../components/myHome/MyVideo.vue'], resolve),
            meta: {
              title: '收藏视频',
            },
          },
          {
            path: "myall",
            name: "myallher",
            component: resolve => require(['../components/myHome/MyAll.vue'], resolve),
            meta: {
              title: '主页',
            },
          },
          {
            path: "myconcern",
            name: "myconcernher",
            component: resolve => require(['../components/myHome/MyConcern.vue'], resolve),
            meta: {
              title: '关注列表',
            },
          },
          {
            path: "myfans",
            name: "myfansher",
            component: resolve => require(['../components/myHome/MyFans.vue'], resolve),
            meta: {
              title: '粉丝列表',
            },
          },
        ]
      },
      {
        path: "diseasedetails/:id",
        name: "DiseaseDetails",
        component: DiseaseDetails,
        meta: {
          requireAuth: false,
          title: '疾病详情'
        },
      },

      {
        path: "knowledgedetail",
        name: "knowledgedetail",
        component: resolve => require(['../views/KnowledgeDetail.vue'], resolve),
        meta: {
          requireAuth: false,
          title: '疾病知识详情'
        },
      },

      {
        path: "videodetail",
        name: "videodetail",
        component: resolve => require(['../views/VideoDetail.vue'], resolve),
        meta: {
          requireAuth: false,
          title: '疾病视频详情'
        },
      },
      {
        path: "login",
        name: "login",
        component: resolve => require(['../views/Login.vue'], resolve),
        meta: {
          requireAuth: false,
          title: '登录'
        },
      },
      ,
      {
        path: "myexp",
        name: "myexp",
        component: resolve => require(['../views/MyExp.vue'], resolve),
        meta: {
          requireAuth: true,
          title: '我的经验'
        },
        children: [

          {
            path: "expdra",
            name: "expdra",
            component: resolve => require(['../components/ExpDra.vue'], resolve),
            meta: {
              requireAuth: true,
              title: '发布经验'
            },
          },
          {
            path: "colexp",
            name: "colexp",
            component: resolve => require(['../components/ColExp.vue'], resolve),
            meta: {
              requireAuth: true,
              title: '收藏经验'
            },
          },
          {
            path: "relexpr",
            name: "relexpr",
            component: resolve => require(['../components/RelExpr.vue'], resolve),
            meta: {
              requireAuth: true,
              title: '经验草稿'
            },
          },

        ]
      },
      {
        path: "releaseexp",
        name: "releaseexp",
        component: resolve => require(['../views/ReleaseExp.vue'], resolve),
        
        meta: {
          requireAuth: true,
          title: '发布经验'
        },
      },
      {
        path: "explabel",
        name: "explabel",
        component: resolve => require(['../views/ExpLabel.vue'], resolve),
        meta: {
          requireAuth: true,
          title: '经验标签'
        },
      },
      {
        path: "accset",
        name: "accset",
        component: resolve => require(['../views/AccSet.vue'], resolve),
        meta: {
          requireAuth: true,
          title: '账户设置'
        },
        children: [

          {
            path: "myinfo",
            name: "myinfo",
            component: resolve => require(['../components/MyInfo.vue'], resolve),
          },
          {
            path: "accinfo",
            name: "accinfo",
            component: resolve => require(['../components/AccInfo.vue'], resolve),
          },
          {
            path: "priinfo",
            name: "priinfo",
            component: resolve => require(['../components/PriInfo.vue'], resolve),
          },

        ]
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
//前置守卫：验证是否登录进行跳转
router.beforeEach((to, from, next) => {
  // console.log(to)
  let arr = to.matched;
  let requireAuth = false;
  arr.forEach(item => {
    if (item.meta.requireAuth == true) {
      requireAuth = true;
    }
  })
  let isLogin = JSON.parse(sessionStorage.getItem( 'isLogin'));
  if (requireAuth && !isLogin) {
    if (to.fullPath == '/main/login') {
      next()
    } else {
      next({ path: '/main/login' })
    }
  } else {
    next()
  }

})
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
  window.document.title=to.meta.title
});

export default router;
